package com.sinoma.G2project.MoYu.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.firenay.common.utils.PageUtils;
import com.firenay.common.utils.Query;
import com.sinoma.G2project.MoYu.Dao.member.MemberDao;
import com.sinoma.G2project.MoYu.Service.MemberService;
import com.sinoma.G2project.MoYu.exception.RegisterEnum;
import com.sinoma.G2project.MoYu.vo.UserRegisterVo;
import com.sinoma.G2project.config.datasource.DataSwitch;
import com.sinoma.G2project.MoYu.entity.MemberEntity;
import com.sinoma.G2project.MoYu.exception.PhoneExistException;
import com.sinoma.G2project.MoYu.exception.UserNameExistException;
import com.sinoma.G2project.MoYu.vo.UserLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Slf4j
@Service("memberService")
@DataSwitch("Member")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void checkPhone(String phone) throws PhoneExistException {
        if (this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone)) > 0) {
            throw new PhoneExistException();
        }
    }

    @Override
    public void checkUserName(String username) throws UserNameExistException {
        if (this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username)) > 0) {
            throw new UserNameExistException();
        }
    }

    @Override
    public MemberEntity login(UserLoginVo vo) {
        String loginacct = vo.getLoginacct();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        // 去数据库查询
        MemberEntity entity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginacct).or().eq("mobile", loginacct));
        if (entity == null) {
            // 登录失败
            return null;
        } else {
            // 前面传一个明文密码 后面传一个编码后的密码
            boolean matches = bCryptPasswordEncoder.matches(vo.getPassword(), entity.getPassword());
            if (matches) {
                entity.setPassword(null);
                return entity;
            } else {
                return null;
            }
        }
    }

    @Override
    public String register(UserRegisterVo vo) {
        String msg = "";
        if (!vo.getPasswordc().equals(vo.getPassword()) ||
                "".equals(vo.getUsername())  ||
                "".equals(vo.getPassword())
        ) {
            msg = RegisterEnum.EMPTY.name();
            }
        else{

            MemberEntity entity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", vo.getUsername()).or().eq("mobile", vo.getMobile()));
            MemberEntity memberEntity = new MemberEntity();
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            String encode = bCryptPasswordEncoder.encode(vo.getPassword());
            if (entity == null) {

                BeanUtils.copyProperties(vo,memberEntity);

                memberEntity.setPassword(encode);
                this.baseMapper.insert(memberEntity);
                msg = RegisterEnum.SUCCESS.name();
            }else{
                if (vo.getCover()==1){
                    BeanUtils.copyProperties(vo,entity);
                    entity.setPassword(encode);
                    this.baseMapper.updateById(entity);
                    msg = RegisterEnum.SUCCESS.name();
                }else {
                    msg = RegisterEnum.REPEAT.name();
                }
            }
        }
        return msg;

    }
}